<template>
  <div>
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="false"
      label-width="68px"
    >
      <el-form-item label="标签组名" prop="labelGroupName">
        <el-input
          v-model.trim="queryParams.labelGroupName"
          placeholder="请输入标签组名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="标签名" prop="labelName">
        <el-input
          v-model.trim="queryParams.labelName"
          placeholder="请输入标签名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="逻辑删除" prop="isDelete">
        <el-input
          v-model.trim="queryParams.isDelete"
          placeholder="请输入逻辑删除"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-dialog
      :visible.sync="openOrderNumber"
      append-to-body
      title="新增标签组"
      width="500px"
      v-if="openOrderNumber"
      @submit.native.prevent
    >
      <el-form ref="formApply" :model="formApply" label-width="100px">
        <el-form-item label="标签组名" prop="contactPerson">
          <el-input
            style="width: 200px"
            v-model="formApply.contactPerson"
            placeholder=""
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" style="margin-top: 50px">
        <el-button type="primary" @click="handleaddBiTalentLabelGroup"
          >确 定</el-button
        >

        <!-- <el-button @click="cancelApply">取消</el-button> -->
      </div>
    </el-dialog>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="openOrderNumber = true"
          >新增标签组</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['talent:BiTalentLabelGroup:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['talent:BiTalentLabelGroup:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['talent:BiTalentLabelGroup:export']"
          >导出</el-button
        >
      </el-col> -->
      <!-- <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar> -->
    </el-row>

    <el-table
      v-loading="loading"
      :data="BiTalentLabelGroupList"
      @selection-change="handleSelectionChange"
      @cell-click="tableEdit"
      :row-class-name="tableRowClassName"
      @row-click="handleRowClick"
      :height="550"
    >
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <!-- <el-table-column label="id" align="center" prop="id" /> -->
      <el-table-column label="标签组名" align="center" prop="labelGroupName" />
      <!-- <el-table-column label="标签名" align="center" prop="labelName" /> -->
      <!-- <el-table-column label="逻辑删除" align="center" prop="isDelete" />
      <el-table-column label="备注" align="center" prop="remark" /> -->
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleupdateLabelGroup(scope.row)"
            >修改标签名</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
      class="simple-pagination"
    />

    <!-- <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    /> -->
    <el-dialog
      :title="title"
      :visible.sync="openupdate"
      width="500px"
      append-to-body
    >
      <el-form
        ref="formupdate"
        :model="formupdate"
        :rules="rules"
        label-width="120px"
      >
        <el-form-item label="旧标签组名" prop="labelGroupName">
          <el-input
            :disabled="true"
            v-model.trim="formupdate.labelGroupNameOld"
            placeholder="请输入标签组名"
          />
        </el-form-item>
        <el-form-item label="新标签组名" prop="labelName">
          <el-input
            v-model.trim="formupdate.labelGroupNameNew"
            placeholder="请输入标签名"
          />
        </el-form-item>
        <!-- <el-form-item label="逻辑删除" prop="isDelete">
          <el-input v-model.trim="form.isDelete" placeholder="请输入逻辑删除" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model.trim="form.remark" placeholder="请输入备注" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFormupdate">确 定</el-button>
        <!-- <el-button @click="cancel">取 消</el-button> -->
      </div>
    </el-dialog>

    <!-- 添加或修改达人标签组对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标签组名" prop="labelGroupName">
          <el-input
            :disabled="true"
            v-model.trim="form.labelGroupName"
            placeholder="请输入标签组名"
          />
        </el-form-item>
        <el-form-item label="标签名" prop="labelName">
          <!-- <el-input
            v-model.trim="form.labelNameList"
            placeholder="请输入标签名"
          /> -->
          <el-select
            v-model="form.labelNameList"
            placeholder=""
            filterable
            multiple
          >
            <el-option
              v-for="dict in TalentLabelList"
              :key="dict"
              :label="dict"
              :value="dict"
            />
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="逻辑删除" prop="isDelete">
          <el-input v-model.trim="form.isDelete" placeholder="请输入逻辑删除" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model.trim="form.remark" placeholder="请输入备注" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listBiTalentLabelGroup,
  getBiTalentLabelGroup,
  delBiTalentLabelGroup,
  addBiTalentLabelGroup,
  updateBiTalentLabelGroup,
  labelGroupAddLabel,
  deleteLabelGroup,
  updateLabelGroup,
} from "@/api/talent/BiTalentLabelGroup";
import {
  selectLabelGroupList,
  listBiTalentLabel,
  getBiTalentLabel,
  delBiTalentLabel,
  addBiTalentLabel,
  updateBiTalentLabel,
} from "@/api/talent/BiTalentLabel";
import {
  releaseTalent,
  distributionTalent,
  setLabel,
  talentBlock,
  selectTalentLabel,
} from "@/api/talent/Detail";
export default {
  name: "BiTalentLabelGroup",
  data() {
    return {
      tableRowIndex: null,
      formupdate: {},
      openupdate: false,
      formApply: {},
      openOrderNumber: false,
      TalentLabelList: [],
      inputValue: "",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 达人标签组表格数据
      BiTalentLabelGroupList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        labelGroupName: null,
        labelName: null,
        isDelete: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleRowClick(row) {
      this.tableRowIndex = this.getArrayIndex(this.BiTalentLabelGroupList, row); // 获取当前点击行下标　　　 // ... 点击当前行，进行的操作
    },
    tableRowClassName({ row, rowIndex }) {
      if (rowIndex === this.tableRowIndex) {
        return "highlight-row";
      }
      return "";
    },
    // 获取一个元素在数组中的下标
    getArrayIndex(arr, obj) {
      var i = arr.length;
      while (i--) {
        if (arr[i] === obj) {
          return i;
        }
      }
      return -1;
    },
    handleaddBiTalentLabelGroup() {
      addBiTalentLabelGroup({
        labelGroupName: this.formApply.contactPerson,
        labelName: null,
      }).then((response) => {
        this.$modal.msgSuccess("新增成功");
        this.openOrderNumber = false;
        this.getList();
      });
    },
    tableEdit(row, column, cell, event) {
      // this.checkpurchaseDocumentCode = row.purchaseDocumentCode;
      //   this.queryParams.labelGroupName = row.labelGroupName;
      //   this.getList();
      this.$emit("tableEdit", row);
    },
    /** 查询达人标签组列表 */
    getList() {
      this.loading = true;
      selectLabelGroupList(this.queryParams).then((response) => {
        this.BiTalentLabelGroupList = [];
        response.rows.forEach((item) => {
          this.BiTalentLabelGroupList.push({
            labelGroupName: item,
          });
        });
        // this.BiTalentLabelGroupList = response.rows.map((item) => {
        //   labelGroupName: item.labelGroupName;
        // });

        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        // id: null,
        labelGroupName: null,
        labelName: null,
        // createBy: null,
        // createTime: null,
        // updateBy: null,
        // updateTime: null,
        // isDelete: null,
        // remark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加达人标签组";
    },
    handleupdateLabelGroup(row) {
      this.formupdate.labelGroupNameOld = row.labelGroupName;
      this.openupdate = true;
    },
    submitFormupdate() {
      updateLabelGroup(this.formupdate).then((response) => {
        this.$modal.msgSuccess("修改成功");
        this.openupdate = false;
        let row = {
          labelGroupName: this.formupdate.labelGroupNameNew,
        };
        this.tableEdit(row);
        this.getList();
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      //   const id = row.id || this.ids;
      //   getBiTalentLabelGroup(id).then((response) => {
      //     this.form = response.data;
      if (row) {
        this.form.labelGroupName = row.labelGroupName;
      }
      selectTalentLabel().then((response) => {
        this.TalentLabelList = response;
      });
      this.open = true;
      // this.title = "修改达人标签组";
      //   });
    },
    /** 提交按钮 */
    submitForm() {
      this.form.labelNameList = this.form.labelNameList.join(",");
      labelGroupAddLabel(this.form).then((response) => {
        this.$modal.msgSuccess("新增成功");
        this.open = false;
        this.getList();
      });
      //   this.$refs["form"].validate((valid) => {
      //     if (valid) {
      //       if (this.form.id != null) {
      //         updateBiTalentLabelGroup(this.form).then((response) => {
      //           this.$modal.msgSuccess("修改成功");
      //           this.open = false;
      //           this.getList();
      //         });
      //       } else {
      //         addBiTalentLabelGroup(this.form).then((response) => {
      //           this.$modal.msgSuccess("新增成功");
      //           this.open = false;
      //           this.getList();
      //         });
      //       }
      //     }
      //   });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = { labelGroupName: row.labelGroupName };
      this.$modal
        .confirm("是否确认删除达人标签组的数据项？")
        .then(function () {
          return deleteLabelGroup(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "talent/BiTalentLabelGroup/export",
        {
          ...this.queryParams,
        },
        `BiTalentLabelGroup_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style>
tr.highlight-row td {
  background-color: #fffbe0;
  color: #06aea6;
}
</style>
<style lang="scss" scoped>
.simple-pagination {
  /* 隐藏中间页码（保留首尾） */
  ::v-deep .el-pager {
    > li:not(:first-child):not(:last-child) {
      display: none !important;
    }

    /* 添加省略号 */
    &::after {
      content: "...";
      display: inline-block;
      padding: 8px;
      color: #606266;
    }
  }

  /* 调整首尾页码间距 */
  ::v-deep .el-pager li:first-child {
    margin-right: 5px;
  }
  ::v-deep .el-pager li:last-child {
    margin-left: 5px;
  }

  /* 隐藏原生省略号 */
  ::v-deep .btn-quicknext,
  ::v-deep .btn-quickprev {
    display: none !important;
  }
}
/* 调整总条数位置 */
// .simple-pagination ::v-deep .el-pagination__total {
//   float: right;
// }

// /* 输入框样式优化 */
// .simple-pagination ::v-deep .el-pagination__jump {
//   margin-left: 0;
// }
</style>